<template>
  <div class="score-case">
    <div class="table">
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>科目</th>
            <th>成绩</th>
            <th>考试时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <Base :list="list" />
        
        <Main :list="list" @subtract="subtractFn" />
      </table>
    </div>
    <Add @add="addFn" />
  </div>
</template>

<script>
//导入注册
import Add from "./components/chengJi-uadd.vue";
import Main from "./components/chengJi-main.vue";
import Base from "./components/chengJi-base.vue";
export default {
  components: {
    Main,
    Base,
    Add,
  },
  name: "ScoreCase",
  watch: {
    list: {
      immediate: true,
      deep: true,
      handler(Val) {
        localStorage.setItem("gradeList", JSON.stringify(Val));
      },
    },
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem("gradeList")) || [],
      subject: "",
      score: "",
    };
  },
  methods: {
    subtractFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    addFn(subject, score) {
      console.log(subject, score);
      if (score >= 0 && score <= 100 && subject && score !== "") {
        this.list.push({
          id: this.list.length ? this.list.at(-1).id + 1 : 1,
          subject: subject,
          score: score,
          date: new Date(),
        });
        subject = "";
        score = "";
      } else {
        alert("分数或科目错误");
      }
    },
  },
};
</script>

<style lang="less">
.score-case {
  width: 1000px;
  margin: 50px auto;
  display: flex;
  .table {
    flex: 4;
    table {
      width: 100%;
      border-spacing: 0;
      border-top: 1px solid #ccc;
      border-left: 1px solid #ccc;
      th {
        background: #f5f5f5;
      }
      tr:hover td {
        background: #f5f5f5;
      }
      td,
      th {
        border-bottom: 1px solid #ccc;
        border-right: 1px solid #ccc;
        text-align: center;
        padding: 10px;
        &.red {
          color: red;
        }
      }
    }
    .none {
      height: 100px;
      line-height: 100px;
      color: #999;
    }
  }
  .form {
    flex: 1;
    padding: 20px;
    .form-item {
      display: flex;
      margin-bottom: 20px;
      align-items: center;
    }
    .form-item .label {
      width: 60px;
      text-align: right;
      font-size: 14px;
    }
    .form-item .input {
      flex: 1;
    }
    .form-item input,
    .form-item select {
      appearance: none;
      outline: none;
      border: 1px solid #ccc;
      width: 200px;
      height: 40px;
      box-sizing: border-box;
      padding: 10px;
      color: #666;
    }
    .form-item input::placeholder {
      color: #666;
    }
    .form-item .cancel,
    .form-item .submit {
      appearance: none;
      outline: none;
      border: 1px solid #ccc;
      border-radius: 4px;
      padding: 4px 10px;
      margin-right: 10px;
      font-size: 12px;
      background: #ccc;
    }
    .form-item .submit {
      border-color: #069;
      background: #069;
      color: #fff;
    }
  }
}
</style>
